import { legacy_createStore as createStore } from 'redux';

const initState = {
	role: localStorage.getItem('role') || '',
	nickname: localStorage.getItem('nickname') || ''
}
const reducer = (state = initState, action: any) => {
	switch (action.type) {
		case 'modify_user': {
			return {
				role: action.payload.role,
				nickname: action.payload.nickname
			}
		}
	}
	return state
}
const store = createStore(reducer)
// 订阅仓库
store.subscribe(() => {
	// 每一次 只要 dispatch action
	// 该回调函数会执行
	const state = store.getState();
	localStorage.setItem('role', state.role)
	localStorage.setItem('nickname', state.nickname)
})

export default store
